<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="handleFilter"
        :before-open="beforeOpen"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-reset="handleRefreshChange"
        @refresh-change="handleRefreshChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot-scope="scope" slot="create_timeSearch">
          <el-date-picker
            v-model="searchDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {fetchList} from '@/api/admin/pay/search'
import { mapGetters } from "vuex";
import { BigNumber } from 'bignumber.js'
import moment from "moment";

export default {
  name: "client",
  data() {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: searchOption,
      searchDate: ''
    };
  },
  created() {
    if(this.permissions["isCompany"]){
      this.findObject(this.tableOption.column, 'company_id').search = false
    }
  },
  mounted: function() {

  },
  computed: {
    ...mapGetters(["permissions"])
  },
  methods: {
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(response => {
        this.tableData = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.tableData.map(item=>{
          if(item.status === 1){
            if(item.pay_type === 1){
              item.total = BigNumber(item.money).minus(BigNumber(item.charge))
            }else{
              item.total = item.money
            }
            
          }else{
            item.total = '--'
          }
          
        })
        this.tableLoading = false;
      });
    },
    rowDel: function(row, index) {
      this.$confirm("是否确认删除ID为" + row.clientId, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.clientId);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.refreshChange();
        });
    },
    handleUpdate: function(row, index, done) {
      putObj(row).then(data => {
        this.$message.success("修改成功");
        this.refreshChange();
        done();
      });
    },
    handleSave: function(row, done) {
      addObj(row).then(data => {
        this.$message.success("添加成功");
        this.refreshChange();
        done();
      });
    },
    handleFilter(param, done) {
      this.page.currentPage = 1; 

      const qDate = {}
      if(this.searchDate){
        qDate.start = moment(this.searchDate[0]).format("YYYY-MM-DD HH:mm:ss")
        qDate.end = moment(this.searchDate[1]).format("YYYY-MM-DD HH:mm:ss")
      }
      this.getList(this.page, Object.assign(param, qDate));
      done();
      done();
    },
    handleRefreshChange() {
      this.searchDate = ''
      this.getList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    currentChange(current) {
      this.page.currentPage = current;
      this.getList(this.page);
    },
    beforeOpen(show, type) {
      window.boxType = type;
      show();
    },
    goAdd(){
      this.$router.push({
        path: '/pay/manage/add'
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped></style>
